<template>
  <el-dialog title="选择图标" v-model="visible" @close="saveIcon('')">
    <div class="icon-wrapper">
      <div class="icon-list">
        <div
          class="icon-item"
          v-for="(name, index) in Object.keys(ElIcons)"
          :key="index"
          @click="saveIcon(name)"
        >
          <component :is="`el${humpToUnderline(name)}`" />
          <span class="name">{{ `el${humpToUnderline(name)}` }}</span>
        </div>
      </div>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { humpToUnderline } from '@/utils'
import { ref, watch } from 'vue'
import { ElDialog } from 'element-plus'
import * as ElIcons from '@element-plus/icons-vue'

const props = defineProps({
  isShow: {
    type: Boolean,
    default: false
  }
})
const visible = ref<boolean>(false)
watch(
  () => props.isShow,
  (val) => {
    visible.value = val
  },
  { immediate: true }
)

const emits = defineEmits(['close'])

// 点击图标后关闭弹窗，并将图标名称传递给父组件
const saveIcon = (name: string) => {
  let result = ''
  if (name) {
    result = humpToUnderline('el' + name)
  }
  emits('close', result)
}
</script>

<style lang="scss" scoped>
.icon-wrapper {
  height: 500px;
  overflow-y: auto;
  &::-webkit-scrollbar {
    width: 0;
    height: 0;
  }
}
.icon-list {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
}
svg {
  width: 2em;
  height: 2em;
}
.icon-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10px;
  cursor: pointer;
  .name {
    margin-top: 10px;
  }
}
</style>
